package com.example.dreamlist2;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

import android.content.Intent;
import android.os.Bundle;
import android.view.MenuItem;

import com.example.dreamlist2.friend.FriendFragment;
import com.example.dreamlist2.goal.GoalFragment;
import com.example.dreamlist2.me.MeFragment;
import com.example.dreamlist2.todo.TodoFragment;
import com.google.android.material.bottomnavigation.BottomNavigationView;

public class BaseActivity extends AppCompatActivity {

    private FragmentTransaction transaction;
    private FragmentManager fragmentManager;
    StateInfo stateInfo;
    private int lastfragment = 0;
    private TodoFragment todoFragment = new TodoFragment();
    private MeFragment meFragment = new MeFragment();
    Bundle bundle1;
    //Bundle bundle2 = new Bundle();
    private FriendFragment friendFragment = new FriendFragment();
    private GoalFragment goalFragment = new GoalFragment();
    private BottomNavigationView navView;
    private Fragment[] frag = new Fragment[4];

    public int RESULT_ADD = 1, RESULT_EDIT = 2;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        if (savedInstanceState != null) {
            savedInstanceState.remove("android:fragments");   //注意：基类是Activity时参数为android:fragments， 一定要在super.onCreate函数前执行！！！
        }
        super.onCreate(savedInstanceState);
        setContentView(R.layout.base);
        bundle1 = new Bundle();
        navView = findViewById(R.id.bnv1);
        stateInfo = (StateInfo)getApplication();
        bundle1.putString("uid", stateInfo.getUserin().getObjectId());
        //bundle2.putString("uid", stateInfo.getUserin().getUid());
        todoFragment.setArguments(bundle1);
        goalFragment.setArguments(bundle1);
        friendFragment.setArguments(bundle1);
        //meFragment.setArguments(bundle2;

        frag[0] = todoFragment;
        frag[1] = goalFragment;
        frag[2] = friendFragment;
        frag[3] = meFragment;

        ActionBar actionBar = getSupportActionBar();
        actionBar.hide();

        setDefaultFragment();
        navView.setOnNavigationItemSelectedListener( mOnNavigationItemSelectedListener );
    }


    //切换监听
    private BottomNavigationView.OnNavigationItemSelectedListener mOnNavigationItemSelectedListener
            = new BottomNavigationView.OnNavigationItemSelectedListener() {

        @Override
        public boolean onNavigationItemSelected(@NonNull MenuItem item) {
            fragmentManager = getSupportFragmentManager();  //使用fragmentmanager和transaction来实现切换效果
            transaction = fragmentManager.beginTransaction();
            switch (item.getItemId()) {
                case R.id.bnv1_todo:
                    if(lastfragment != 0){
                        transaction.hide(frag[lastfragment]);
                        lastfragment = 0;
                        if (!frag[0].isAdded()) {
                            transaction.add(R.id.indexview, frag[0]);
                        }
                        transaction.show(frag[0]).commit();
                    }
                    //transaction.replace(R.id.content,todoFragment).show(todoFragment);  //对应的fragment
                    //transaction.commit();  //一定不要忘记commit，否则不会显示
                    return true;
                case R.id.bnv1_habit:
                    if(lastfragment != 1){
                        transaction.hide(frag[lastfragment]);
                        lastfragment = 1;
                        if (!frag[1].isAdded()) {
                            transaction.add(R.id.indexview, frag[1]);
                        }
                        transaction.show(frag[1]).commit();
                    }
                    return true;
                case R.id.bnv1_friend:
                    if(lastfragment != 2){
                        transaction.hide(frag[lastfragment]);
                        lastfragment = 2;
                        if (!frag[2].isAdded()) {
                            transaction.add(R.id.indexview, frag[2]);
                        }
                        transaction.show(frag[2]).commit();
                    }
                    return true;
                case R.id.bnv1_me:
                    if(lastfragment != 3){
                        transaction.hide(frag[lastfragment]);
                        lastfragment = 3;
                        if (!frag[3].isAdded()) {
                            transaction.add(R.id.indexview, frag[3]);
                        }
                        transaction.show(frag[3]).commit();
                    }
                    //transaction.replace(R.id.content, meFragment).show(meFragment);
                    //transaction.commit();
                    return true;
            }
            return false;
        }
    };

    // 设置默认显示的页面
    private void setDefaultFragment(){
        fragmentManager = getSupportFragmentManager();
        transaction = fragmentManager.beginTransaction();
        transaction.add(R.id.indexview,todoFragment);
        lastfragment = 0;
        transaction.commit();
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        //如果返回值等于ADD或者EDIT 就进入todolist
        if (resultCode == RESULT_ADD || resultCode == RESULT_EDIT) {
            fragmentManager = getSupportFragmentManager();  //使用fragmentmanager和transaction来实现切换效果
            transaction = fragmentManager.beginTransaction();
            transaction.replace(R.id.indexview, todoFragment).show(todoFragment);
            transaction.commit();
        }
    }

}